<?php
/**
 * @file			admgr.ctrl.php
 * @CopyRight		(C)1996-2099 SINA Inc.
 * @Project			Vyouji
 * @Author			guanghui <guanghui1@staff.sina.com.cn>
 * @Create Date:	2011-10-27
 * @Modified By:	guanghui/2011-10-27
 * @Brief			焦点图管理控制器
 */

include(P_ADMIN_MODULES . '/admin.base.php');

class admgr_ctrl extends admin_base
{
	/**
	* 导航菜单，数组形式
	* 
	* @var mixed
	*/
	private $_navTitle = array(
						1 => array('首页管理', '运营位管理'),
						2 => array('首页管理', '专题推荐')
					);
					
	/**
	* 运营位图片尺寸规格
	* 
	* @var array
	*/
	private $_size = array(
						1 => '230*140',
						2 => '300*120'
					);
	
	/**
	* 运营位管理
	* 
	*/
	function index()
	{
		$type = (int)V('g:type', 0);
		$page = (int)V('g:page', 1);
		$count = 20;
		
		$adList = APP::MOD('advertising')->getList_db($type, ($page - 1) * $count, $count);
		
		$pager = APP::N('pager');
		$page_param = array(
			'currentPage' => $page,
			'pageSize' => $count,
			'recordCount' => $adList['total'],
			'linkNumber' => 10
		);
		$pager->setParam($page_param);
		TPL::assign('pager', $pager->makePageForKeyWord('page', array('type' => $type)));
		
		TPL::assign('type', $type);
		TPL::assign('sizeTip', $this->_size[$type]);
		TPL::assign('navTitle', $this->_navTitle[$type]);
		TPL::assign('list', $adList['list']);
		$this->_display();
	}
	
	/**
	* 添加运营位
	* 
	*/
	function save()
	{
		$id = trim(V('p:id', ''));
		$type = (int)V('p:type', 0);
		$target_url = trim(V('p:target_url', ''));
		$sort = (int)V('p:sort', 0);
		
		if (!isset($this->_navTitle[$type]) || empty($target_url)) {
			$this->_error('提交数据错误', URL('admin/admgr.index'));
		}
		
		$backUrl = URL('admin/admgr.index', 'type=' . $type);
		
		if (empty($id) || (isset($_FILES['image']) && !empty($_FILES['image']['name']))) {
			if (($imageInfo = getimagesize($_FILES['image']['tmp_name'])) === FALSE)
				$this->_error('上传文件不是有效的图片类型', $backUrl);
			
			/*	
			list($width, $height) = explode('*', $this->_size[$type]);
			if ($width != $imageInfo[0] || $height != $imageInfo[1])
				$this->_error('图片尺寸不符合要求，请上传尺寸为' . $this->_size[$type] . '的图片', $backUrl);
			*/
			
			$imgRst = $this->_uploadBase('image');
			if ($imgRst === false) {
				$this->_error('上传图片失败', $backUrl);
			}
			
			if (is_array($imgRst) && isset($imgRst['errno'])) {
				$this->_error('上传图片失败，' . $imgRst['err'], $backUrl);
			}
			
			if (!is_array($imgRst) || !isset($imgRst['original_pic']['url']) || empty($imgRst['original_pic']['url'])) {
				$this->_error('上传图片失败', $backUrl);
			}
			
			$img_url = $imgRst['original_pic']['url'];
		}
		
		$data = array('type' => $type, 'target_link' => $target_url, 'sort' => $sort);
		isset($img_url) && $data['img_src'] = $img_url;
		if (empty($id))	$data['create_time'] = APP_LOCAL_TIMESTAMP;
		
		if (!APP::MOD('advertising')->save($data, $id)) {
			$this->_error('添加失败', $backUrl);
		}
		APP::redirect($backUrl, 3);
	}
	
	/**
	* 删除运营位
	* 
	*/
	function delete()
	{
		$this->_checkReferer();
		
		$id = trim(V('g:id', ''));
		$type = (int)V('g:type', 0);
		if (!isset($this->_navTitle[$type]) || empty($id) || !is_numeric($id)) {
			APP::ajaxRst(FALSE, 9999999, '参数错误');
		}
		
		if (APP::MOD('advertising')->delete($id, $type)) {
			APP::ajaxRst(TRUE);
		} else {
			APP::ajaxRst(FALSE, 9999999, '删除失败');
		}
	}
}
